package healththink
import static org.junit.Assert.*
import org.junit.*

class FacilityIntegrationTests {

    @Before
    void setUp() {
        // Setup logic here
    }

    @After
    void tearDown() {
        // Tear down logic here
    }

	@Test
    void testFacilityIntegration() {
		
		Facility fac = HealthThink.getStaticFacility()
		fac.facilitySite = HealthThink.getStaticFacilitySite()
		if (!fac.save(flush: true))
		{
			fail("Could not save facility")
		}
		Facility expectedFacility = Facility.findByName(fac.name)
		assertEquals("Facilities Not equal", fac, expectedFacility)
		
		
        User user = HealthThink.getStaticUser()
		if (!user.save(flush: true))
		{
			fail("Could not save user")
		}
		User expectedUser = User.findByUsername(user.username)
		assertEquals("Users Not equal", user, expectedUser)
		

		Attribute fa = new Attribute(
			isPublicType: false,
			name: "Test Attribute One",
			description:"Test Attribute description",
			user: user,
			userType: UserType.Contributor
			)
		fa = fa.save(flush: true)
		fa = Attribute.findById(fa.id)

		FacilityUserLink ful = FacilityUserLink.link(expectedUser, expectedFacility)
		ful.userType = UserType.Contributor
		ful.save(flush: true)
		
		expectedFacility.addToFacilityUsers(expectedUser)
		expectedFacility.save(flush: true)

		FacilityAttributeRating far = new FacilityAttributeRating(
			rating: 10,
			user: user,
			userType: UserType.Moderator
		)

		FacilityAttributeLink fal = FacilityAttributeLink.link(fa, fac)
		fal.addToFacilityAttributeRatings(far)
		fal.save(flush: true)
		
		FacilityAttributeLink falTarget = FacilityAttributeLink.findById(fal.id)
		assertEquals("Facilities Not equal", fal, falTarget)
		falTarget.save(flush: true)

		Facility expectedFacility1 = Facility.findByName(fac.name)
		assertEquals("Facilities Not equal", fac, expectedFacility1)
		expectedFacility1.save(flush: true)
		
		Address adtarget = HealthThink.getStaticAddress()
	    assertNotNull("Target Address Null?", adtarget)
	    fac.addToAddresses(adtarget)
	    fac = fac.save(flush:true)
	   
	    Address expected = Address.findById(adtarget.id)
	    assertNotNull("Expected Address Null?", expected)
	    assertEquals("Address Not equal", adtarget, expected)

		//user.delete(flush:true)
		//fac.delete(flush: true)

    }
}
